<template>
  <div class="mod-category">
    <el-form>
      <el-form-item>
        <el-alert
          :title="this.$i18n.t('publics.alertMessage1')"
          type="warning"
          show-icon
          :closable="false">
        </el-alert>
      </el-form-item>
    </el-form>
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-button
          v-if="isAuth('prod:category:save')"
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="addOrUpdateHandle()"
        >{{$t("brand.add")}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border row-key="categoryId" style="width: 100%;">
      <el-table-column
        prop="categoryName"
        header-align="center"
        treeKey="categoryId"
        width="150"
        :label="this.$i18n.t('publics.category')"
      ></el-table-column>
      <el-table-column
        prop="pic"
        header-align="center"
        align="center"
        :label="this.$i18n.t('publics.image')"
      >
        <template slot-scope="scope">
          <img :src="resourcesUrl + scope.row.pic " height="100" v-if="scope.row.grade !== 1"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        :label="this.$i18n.t('publics.status')"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">{{$t("brand.offline")}}</el-tag>
          <el-tag v-else size="small">{{$t("brand.normal")}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="seq"
        header-align="center"
        align="center"
        :label="this.$i18n.t('hotSearch.seq')"
      ></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        :label="this.$i18n.t('crud.menu')"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('prod:category:update')"
            type="primary"
            size="small"
            @click="addOrUpdateHandle(scope.row.categoryId)"
          >{{$t("brand.revise")}}</el-button>
          <el-button
            v-if="isAuth('prod:category:delete')"
            type="danger"
            size="small"
            @click="deleteHandle(scope.row.categoryId)"
          >{{$t("brand.delete")}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './category-add-or-update'
import { treeDataTranslate } from '@/utils'

export default {
  data () {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/prod/category/listCategory'),
        method: 'get',
        params: this.$http.adornParams({
          maxGrade: 2
        })
      }).then(({ data }) => {
        console.log(data)
        this.dataList = treeDataTranslate(data, 'categoryId', 'parentId')
        console.log(this.dataList)
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('remindPop.makeSure') + ' ' + `[${this.$i18n.t('remindPop.delete')}]` + ' ' + this.$i18n.t('remindPop.operation') + '？', this.$i18n.t('remindPop.remind'), {
        confirmButtonText: this.$i18n.t('remindPop.confirm'),
        cancelButtonText: this.$i18n.t('remindPop.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/prod/category/${id}`),
          method: 'delete',
          data: this.$http.adornData()
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('remindPop.success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      })
    }
  }
}
</script>
<style lang="scss">
.mod-category {
  /deep/ .el-table__fixed-right {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  /deep/ .el-table__fixed{
    height: 100% !important; //设置高优先，以覆盖内联样式
  }

  img {
    height: 80px;
  }
  .el-alert__content {
    line-height: 1.5em;
  }
}
</style>
